package com.moxiao.yfjh.module.activity.controller.admin.activityEnroll;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;
import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;
import com.moxiao.yfjh.module.activity.controller.admin.activityEnroll.vo.*;
import com.moxiao.yfjh.module.activity.convert.activityEnroll.ActivityEnrollConvert;
import com.moxiao.yfjh.module.activity.dal.dataobject.activityEnroll.ActivityEnrollDO;
import com.moxiao.yfjh.module.activity.dal.dataobject.activityInfo.ActivityInfoDO;
import com.moxiao.yfjh.module.activity.service.activityEnroll.ActivityEnrollService;
import com.moxiao.yfjh.module.activity.service.activityInfo.ActivityInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 相亲报名信息")
@RestController
@RequestMapping("/activity/enroll")
@Validated
public class ActivityEnrollController {

    @Resource
    private ActivityEnrollService enrollService;
    @Resource
    private ActivityInfoService infoService;


    @PostMapping("/create")
    @Operation(summary = "创建相亲报名信息")
    @PreAuthorize("@ss.hasPermission('activity:enroll:create')")
    public CommonResult<Long> createEnroll(@Valid @RequestBody ActivityEnrollCreateReqVO createReqVO) {
        return success(enrollService.createEnroll(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新相亲报名信息")
    @PreAuthorize("@ss.hasPermission('activity:enroll:update')")
    public CommonResult<Boolean> updateEnroll(@Valid @RequestBody ActivityEnrollUpdateReqVO updateReqVO) {
        enrollService.updateEnroll(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除相亲报名信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('activity:enroll:delete')")
    public CommonResult<Boolean> deleteEnroll(@RequestParam("id") Long id) {
        enrollService.deleteEnroll(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得相亲报名信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('activity:enroll:query')")
    public CommonResult<ActivityEnrollRespVO> getEnroll(@RequestParam("id") Long id) {
        ActivityEnrollDO enroll = enrollService.getEnroll(id);
        return success(ActivityEnrollConvert.INSTANCE.convert(enroll));
    }

    @GetMapping("/list")
    @Operation(summary = "获得相亲报名信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('activity:enroll:query')")
    public CommonResult<List<ActivityEnrollRespVO>> getEnrollList(@RequestParam("ids") Collection<Long> ids) {
        List<ActivityEnrollDO> list = enrollService.getEnrollList(ids);
        return success(ActivityEnrollConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得相亲报名信息分页")
    @PreAuthorize("@ss.hasPermission('activity:enroll:query')")
    public CommonResult<PageResult<ActivityEnrollRespVO>> getEnrollPage(@Valid ActivityEnrollPageReqVO pageVO) {
        PageResult<ActivityEnrollDO> pageResult = enrollService.getEnrollPage(pageVO);
        return success(ActivityEnrollConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出相亲报名信息 Excel")
    @PreAuthorize("@ss.hasPermission('activity:enroll:export')")
    @OperateLog(type = EXPORT)
    public void exportEnrollExcel(@Valid ActivityEnrollExportReqVO exportReqVO,
                                  HttpServletResponse response) throws IOException {
        List<ActivityEnrollDO> list = enrollService.getEnrollList(exportReqVO);
        // 导出 Excel
        List<ActivityEnrollExcelVO> datas = ActivityEnrollConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "相亲报名信息.xls", "数据", ActivityEnrollExcelVO.class, datas);
    }

    /**
     * 获得活动报名列表
     */
    @GetMapping("/getActivityEnrollList")
    @Operation(summary = "获得活动报名列表")
    @Parameters({
            @Parameter(name = "activityId", description = "活动id", required = true, example = "1"),
            @Parameter(name = "activeSource", description = "活动来源 0 个人活动 1 官方活动", required = true, example = "1024")
    })
    public CommonResult<ActivityEnrollWoMenVo> getActivityEnrollList(@RequestParam("activityId") Long activityId,
                                                                     @RequestParam("activeSource") String activeSource) {
        ActivityEnrollWoMenVo enroll = new ActivityEnrollWoMenVo();
        // 男生
        ActivityEnrollQueryVO menQuery = new ActivityEnrollQueryVO();
        menQuery.setActivityId(activityId);
        menQuery.setActiveSource(activeSource);
        menQuery.setSex((byte) 1);
        List<EnrollUseVO> menList = enrollService.getActivityEnrollList(menQuery);
        enroll.setMenList(menList);
        // 女生
        ActivityEnrollQueryVO womenQuery = new ActivityEnrollQueryVO();
        womenQuery.setActivityId(activityId);
        womenQuery.setActiveSource(activeSource);
        womenQuery.setSex((byte) 2);
        List<EnrollUseVO> womenList = enrollService.getActivityEnrollList(womenQuery);
        enroll.setWomenList(womenList);
        // 活动发起人
        ActivityEnrollQueryVO promoterQuery = new ActivityEnrollQueryVO();
        promoterQuery.setActivityId(activityId);
        ActivityInfoDO infoDO = infoService.getInfo(activityId);
        if (Objects.nonNull(infoDO)) {
            if (infoDO.getActiveSource().equals("0")) {
                promoterQuery.setUserId(infoDO.getHeadUserId());
                List<EnrollUseVO> promoterList = enrollService.getActivityEnrollList(promoterQuery);
                enroll.setPromoter(promoterList);
            }
        }
        return success(enroll);
    }
}
